import { Color, Component, Enum, Font, Label, Node, Sprite, SpriteFrame, _decorator } from "cc";

enum MultiType {
    Sprite,
    Node,
    Font,
    Color
}

const { menu, ccclass, property, disallowMultiple } = _decorator;
@ccclass
@disallowMultiple
@menu("自定义插件/数组插件")
export default class MultiPlug extends Component {	
    @property({type:Enum(MultiType),displayName:'类型',tooltip:"选择类型"})
    select:MultiType = MultiType.Sprite;
    @property({type: SpriteFrame, tooltip: '图片精灵',visible(){return this.select == MultiType.Sprite}})
    sprites: SpriteFrame[] = [];
    @property({type: Node, tooltip: '节点',visible(){return this.select == MultiType.Node}})
    nodes: Node[] = [];
    @property({type: Font, tooltip: '字体',visible(){return this.select == MultiType.Font}})
    fonts: Font[] = [];
    @property({type: Color, tooltip: '颜色',visible(){return this.select == MultiType.Color}})
    colors: Color[] = [];

    public setValue(index: number | string) {
        if (this.fonts.length > 0) {
            console.assert(index < this.fonts.length);
            this.getComponent(Label).font = this.fonts[index];
        } else if (this.sprites.length > 0) {
            console.assert(index < this.sprites.length);
            this.getComponent(Sprite).spriteFrame = this.sprites[index];
        } else if (this.nodes.length > 0) {
            if (typeof (index) === 'number') {
                console.assert(index < this.nodes.length);
                this.nodes.forEach((it, i) => {
                    it.active = (i === index);
                });
            } else {
                this.nodes.forEach((it: Node) => {
                    it.active = (it.name === index);
                })
            }
        } else if (this.colors.length > 0){
            console.assert(index < this.colors.length);
            const render:any = this.getComponent(Label) || this.getComponent(Sprite);
            render && (render.color = this.colors[index]);
        }
    };

    public getValue(index: number): Font | Sprite | Node | Color{
        if (this instanceof Label) {
            console.assert(index < this.fonts.length);
            return this.fonts[index];
        } else if (this instanceof Sprite) {
            console.assert(index < this.sprites.length);
            return this.sprites[index];
        } else if (this instanceof Node) {
            console.assert(index < this.nodes.length);
            return this.nodes[index];
        }
        return null;
    }

    public getColor(index:number):Color{
        console.assert(index < this.colors.length);
        return this.colors[index];
    }
}